/**
 * Visual Blocks Language
 *
 * Copyright 2012 Google Inc.
 * http://blockly.googlecode.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Generating Arduino for math blocks.
 * @author gasolin@gmail.com  (Fred Lin)
 */
'use strict';

goog.provide('Blockly.Arduino.math');

goog.require('Blockly.Arduino');


Blockly.Arduino.math_whole_number = function() {
  // Numeric value.
  var code = window.parseFloat(this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Arduino.math_positive_number = function() {
  // Numeric value.
  var code = window.parseFloat(this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};
Blockly.Arduino.math_angle = function() {
  // Numeric value.
  var code = window.parseFloat(this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.math_number = function() {
  // Numeric value.
  var code = window.parseFloat(this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};



Blockly.Arduino.math_show_number = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'number_values',Blockly.Arduino.ORDER_ATOMIC) || 0;
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Arduino.operator_add = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'NUM1',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.Arduino.valueToCode(this, 'NUM2',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var code = argument0+'+'+argument1;
  return [code,Blockly.Arduino.ORDER_NONE];
};


Blockly.Arduino.operator_subtract = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'NUM1',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.Arduino.valueToCode(this, 'NUM2',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var code = argument0+'-'+argument1;
  return [code,Blockly.Arduino.ORDER_NONE];
};


Blockly.Arduino.operator_multiply = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'NUM1',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.Arduino.valueToCode(this, 'NUM2',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var code = argument0+'*'+argument1;
  return [code,Blockly.Arduino.ORDER_NONE];
};


Blockly.Arduino.operator_divide = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'NUM1',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.Arduino.valueToCode(this, 'NUM2',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var code = argument0+'/'+argument1;
  return [code,Blockly.Arduino.ORDER_NONE];
};


Blockly.Arduino.operator_mapping = function() {
  var value_map_value = Blockly.Arduino.valueToCode(this, 'map_value', Blockly.Arduino.ORDER_ATOMIC);
  var value_map_top = Blockly.Arduino.valueToCode(this, 'map_top', Blockly.Arduino.ORDER_ATOMIC);
  var value_map_bottom = Blockly.Arduino.valueToCode(this, 'map_bottom', Blockly.Arduino.ORDER_ATOMIC);
  var value_to_top = Blockly.Arduino.valueToCode(this, 'to_top', Blockly.Arduino.ORDER_ATOMIC);
  var value_to_bottom = Blockly.Arduino.valueToCode(this, 'to_bottom', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = 'map('+value_map_value+','+value_map_top+','+value_map_bottom+','+value_to_top+','+value_to_bottom+')';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_NONE];
};

Blockly.Arduino.operator_random = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'FROM',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.Arduino.valueToCode(this, 'TO',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var code = 'random('+argument0+','+ argument1+')';
  return [code,Blockly.Arduino.ORDER_NONE];
};
